import React from 'react';
import { Row, Button } from 'antd';

function buildButton(authority){
    const toolbarList = [
        {
            type: "primary",
            icon: "plus",
            event: "onAdd",
            name:"新增"
        }, {
            type: "primary",
            icon: "edit",
            event: "onEdit",
            name: "编辑"
        }, {
            type: "primary",
            icon: "close",
            event: "onDelete",
            name: "删除"
        }
    ];
    let s = 0;
    const toolbar = [];
    for (let i = 0; i < toolbarList.length; i++) {
        s = authority & (1 << i);
        if (s !== 0) {
            toolbar.push(toolbarList[i]);
        }
    }
    return toolbar;
}

const ButtonGroup = ({ authority, ...eventProps }) => {
    const buttonList = buildButton(authority);

    buttonList.forEach((item) => {
        for (let m in eventProps){
            if (item.event === m){
                item.event = eventProps[m];
            }
        }
    });

    return (
        <Row>
            {
                buttonList.map((item,index) => {
                    return (
                        <Button key={index} onClick={item.event} type={item.type} icon={item.icon}>{item.name}</Button>
                    )
                })
            }
        </Row>
    )
};

export default ButtonGroup;